// Device/pages/Info/index.js
/**
 * 设备详情页
 * 
 * Create & edit by cyj at 2024/9/12
 * */
const app = getApp();
import {
    fetchDeviceInfo,
    fetchUpdateName,
    fetchUnpairDevice
} from '@/services/device'
import {
    showToast,
    getParams
} from '@/utils/util';
import { customNavigate } from '@/utils/route'

const deviceComponentMap = new Map([
    [0, {
        'X1_S': 'band-component',
        'X1': 'band-component',
        'X5': 'band-component',
        'TWC1': 'band-component'
    }],
    [1, {
        'R2': 'gas-component',
        'R6': 'gas-component'
    }],
    [2, {
        'DX1': 'fall-component',
        'DD_T': 'tyfall-component'
    }],
    [5, {
        'DL1': 'alertor-component',
        'DL2': 'alertor-component'
    }],
    [6, {
        'TZ1': 'alertor-component'
    }],
    [7, {
        'TZ1_01': 'alertor-component'
    }],
    [8, {
        'X2A': 'bpgauge-component',
        'A666G': 'bpgauge-component',
        'YE620AR': 'bpgauge-component'
    }],
    [9, {
        'T808': 'glucometer-component',
        'G777G': 'glucometer-component',
        '302G': 'glucometer-component'
    }],
    [13, {
        'RC_T': 'existence-component'
    }],
    [14, {
        'SM_T': 'sleep-component'
    }]
])
let menus = [{
    path: '/DeviceBind/pages/Map/index',
    name: '活动轨迹',
    icon: app.globalData.STATIC_PREFIX + '/nav/icon_nav_polyline.png'
}, {
    path: '/DeviceBind/pages/Map/index',
    name: '电子围栏',
    icon: app.globalData.STATIC_PREFIX + '/nav/icon_nav_polygon.png'
}, {
    path: '/DeviceBind/pages/Verify/index',
    name: '设备设置',
    icon: app.globalData.STATIC_PREFIX + '/nav/icon_nav_setting.png'
}, {
    path: '/Notice/pages/index',
    name: '紧急联系人',
    icon: app.globalData.STATIC_PREFIX + '/nav/icon_nav_contact.png'
}, {
    path: '/Device/pages/Version/index',
    name: '设备版本',
    icon: app.globalData.STATIC_PREFIX + '/nav/icon_nav_version.png'
}, {
    path: '/pages/Test/index',
    name: '意外保险',
    icon: app.globalData.STATIC_PREFIX + '/nav/icon_nav_serve.png'
}]
Page({
    /**
     * 页面的初始数据
     */
    data: {
        safeAreaBottom: app.globalData.IPHONE_BAR_HEIGHT,
        device_info: null,
        device_imei: null,
        currentComponent: null
    },
    async onLoad(options) {
        const {
            device_imei
        } = getParams(options.params);
        this.setData({
            device_imei
        }, () => {
            this.queryDeviceInfo(device_imei)
        })
    },
    onShow() {

    },
    async queryDeviceInfo(device_imei) {
        try {
            const res = await fetchDeviceInfo({
                device_imei
            });
            menus = menus.filter((item) => {
                let filterCondition;
                if (res.data.device_type === 0) {
                    filterCondition = item.name !== '意外保险';
                } else if (res.data.device_type === 2) {
                    if(res.data.product_type == 'DX1') {
                        filterCondition = item.name !== '活动轨迹' && item.name !== '电子围栏';
                    } else {
                        filterCondition = item.name !== '活动轨迹' && item.name !== '电子围栏' && item.name !== '意外保险';
                    }
                } else if (res.data.device_type === 7) {
                    filterCondition = item.name !== '活动轨迹' && item.name !== '电子围栏' && item.name !== '设备设置' && item.name !== '紧急联系人' && item.name !== '设备版本' && item.name !== '意外保险';
                } else {
                    filterCondition = item.name !== '活动轨迹' && item.name !== '电子围栏' && item.name !== '意外保险';
                }
                return filterCondition;
            })
            if (res.return_code == 'SUCCESS') {
                this.setData({
                    device_info: res.data,
                    device_imei,
                    currentComponent: deviceComponentMap.get(res.data.device_type)[res.data.product_type],
                    menus,
                    markers: res.data.device_precision && res.data.device_latitude ? [{
                        id: 1,
                        latitude: res.data.device_latitude,
                        longitude: res.data.device_precision,
                        width: 30,
                        height: 45
                    }] : []
                });
            } else {
                showToast('获取设备信息失败')
            }
        } catch (error) {
            showToast('网络错误')
        }
    },
    async onAlertorUpdate(e) {
        const res = await fetchUpdateName({
            ...e.detail,
            device_imei: `${this.data.device_imei}-${e.detail.device_imei}`
        })
        if (res.return_code == 'SUCCESS') {
            showToast('修改配对设备名称成功', () => {
                this.queryDeviceInfo(this.data.device_imei)
            })
        }
    },
    async onAlertorUnpair(e) {
        const res = await fetchUnpairDevice({
            device_imei: this.data.device_imei
        })
        if (res.return_code == 'SUCCESS') {
            showToast('解绑设备成功', () => {
                this.queryDeviceInfo(this.data.device_imei)
            })
        }
    },
    onTapItem(e) {
        const {
            name,
            path
        } = e.currentTarget.dataset.item;
        const {
            device_imei,
            device_type,
            device_name,
            product_type
        } = this.data.device_info;
        if (name == '设备设置') {
            const params = {
                device_type,
                device_name,
                product_type,
                device_imei
            }
            customNavigate({
                url: '/Device/pages/Bind/Verify/index',
                params: {
                    device_params: {
                        ...params
                    },
                    type: 'update'
                }
            })
        } else if (name == '紧急联系人') {
            const params = {
                device_imei: this.data.device_info.device_imei
            }
            customNavigate({
                url: '/Extra/pages/CallCenter/index',
                params
            })
        } else if (name == '活动轨迹') {
            const params = {
                device_imei: this.data.device_info.device_imei,
                type: 'polyline',
                readonly: true
            }
            customNavigate({
                url: '/Device/pages/Bind/Map/index',
                params
            })
        } else if (name == '电子围栏') {
            const params = {
                type: 'polygon',
                readonly: true,
                device_imei: this.data.device_info.device_imei
            }
            customNavigate({
                url: '/Device/pages/Bind/Map/index',
                params
            })
        } else if (name == '设备版本') {
            const params = {
                device_imei: this.data.device_info.device_imei
            }
            customNavigate({
                url: '/Extra/pages/Version/version',
                params
            })
        } else if (name == '意外保险') {
            const params = {
                url: 'https://api.deftech.com.cn/h5/ctyun'
            }
            customNavigate({
                url: '/Extra/pages/WebView/webview',
                params
            })
        }
    },
    onUnload() {
        menus = [{
            path: '/DeviceBind/pages/Map/index',
            name: '活动轨迹',
            icon: app.globalData.STATIC_PREFIX + '/nav/icon_nav_polyline.png'
        }, {
            path: '/DeviceBind/pages/Map/index',
            name: '电子围栏',
            icon: app.globalData.STATIC_PREFIX + '/nav/icon_nav_polygon.png'
        }, {
            path: '/DeviceBind/pages/Verify/index',
            name: '设备设置',
            icon: app.globalData.STATIC_PREFIX + '/nav/icon_nav_setting.png'
        }, {
            path: '/Notice/pages/index',
            name: '紧急联系人',
            icon: app.globalData.STATIC_PREFIX + '/nav/icon_nav_contact.png'
        }, {
            path: '/Device/pages/Version/index',
            name: '设备版本',
            icon: app.globalData.STATIC_PREFIX + '/nav/icon_nav_version.png'
        }, {
            path: '/pages/Test/index',
            name: '意外保险',
            icon: app.globalData.STATIC_PREFIX + '/nav/icon_nav_serve.png'
        }]
    }
})